﻿// <copyright file="ProductDialog.xaml.cs" company="Snowman Consulting">
//     Copyright (c) 2009 Snowman Consulting. All rights reserved.
// </copyright>

namespace SnowmanConsulting.ProductKeyManager.UI
{
    using System.Globalization;
    using System.Windows;
    using System.Windows.Controls;
    using SnowmanConsulting.ProductKeyManager.Data;

    /// <summary>
    /// Interaction logic for NewDialog.xaml
    /// </summary>
    public partial class ProductDialog : Window
    {
        /// <summary>
        /// Product field we work with
        /// </summary>
        private Product product;

        /// <summary>
        /// DialogMode that determines the functionality of the dialog in the current instance
        /// </summary>
        private DialogMode mode = DialogMode.NewProduct;

        /// <summary>
        /// Initializes a new instance of the <see cref="ProductDialog"/> class.
        /// </summary>
        public ProductDialog()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Gets or sets the mode.
        /// </summary>
        /// <value>The current dialog mode.</value>
        public DialogMode Mode
        {
            get { return this.mode; }
            set { this.mode = value; }
        }

        /// <summary>
        /// Gets or sets the product.
        /// </summary>
        /// <value>The product.</value>
        public Product Product
        {
            get { return this.product; }
            set { this.product = value; }
        }

        /// <summary>
        /// Windows the loaded.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void WindowLoaded(object sender, RoutedEventArgs e)
        {
            this.btnNotes.Checked += new RoutedEventHandler(this.NotesButton_Checked);
            this.btnNotes.Unchecked += new RoutedEventHandler(this.NotesButton_Unchecked);
            this.btnSetIcon.Click += new RoutedEventHandler(this.SetIconButton_Click);
            this.lstProductKeys.SelectionChanged += new SelectionChangedEventHandler(this.ProductKeysList_SelectionChanged);
            switch (this.mode)
            {
                case DialogMode.EditProduct:
                    this.Title = "Edit Product";
                    break;
                case DialogMode.ViewProduct:
                    this.Title = "View Product Key(s)";
                    this.txtProductName.IsReadOnly = true;
                    this.AddEditToolbar.Visibility = Visibility.Collapsed;
                    this.btnSetIcon.IsEnabled = false;
                    break;
                case DialogMode.NewProduct:
                default:
                    this.Title = "New Product";
                    this.Product = new Product()
                    {
                        Name = "New Product"
                    };

                    this.Product.Icon = ProductIconHelper.GenericApplicationIcon;

                    break;
            }

            this.lblProductName.Content = string.Format(CultureInfo.CurrentCulture, "{0}:", "Product Name");

            this.SetView();
        }

        /// <summary>
        /// Handles the Click event of the SetIconButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void SetIconButton_Click(object sender, RoutedEventArgs e)
        {
            IconDialog iconDlg = new IconDialog();
            iconDlg.Owner = this;
            bool? result = iconDlg.ShowDialog();

            if ((bool)result)
            {
                this.Product.Icon = iconDlg.SelectedIcon;
            }
        }

        /// <summary>
        /// Handles the SelectionChanged event of the lstProductKeys control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Controls.SelectionChangedEventArgs"/> instance containing the event data.</param>
        private void ProductKeysList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.lstProductKeys.SelectedItem == null)
            {
                this.btnRemoveKeyCode.IsEnabled = false;
            }
            else
            {
                this.btnRemoveKeyCode.IsEnabled = true;
            }
        }

        /// <summary>
        /// Sets the view.
        /// </summary>
        private void SetView()
        {
            this.DataContext = this.Product;
        }

        /// <summary>
        /// Handles the Unchecked event of the btnNotes control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void NotesButton_Unchecked(object sender, RoutedEventArgs e)
        {
            this.pnlProductNotes.Visibility = Visibility.Collapsed;
        }

        /// <summary>
        /// Handles the Checked event of the btnNotes control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void NotesButton_Checked(object sender, RoutedEventArgs e)
        {
            this.pnlProductNotes.Visibility = Visibility.Visible;
        }

        /// <summary>
        /// Adds the key code.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void AddKeyCode(object sender, RoutedEventArgs e)
        {
            ProductKey pk = new ProductKey()
            {
                Code = "New Code",
                AllocatedTo = "Allocated To"
            };

            this.Product.Keys.Add(pk);
            this.lstProductKeys.SelectedItem = pk;
        }

        /// <summary>
        /// Removes the key code.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void RemoveKeyCode(object sender, RoutedEventArgs e)
        {
            if (this.lstProductKeys.SelectedItem != null)
            {
                this.Product.Keys.Remove(this.lstProductKeys.SelectedItem as ProductKey);
            }
        }
    }
}
